package com.gexin.common.utils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class OtherUtil {

	/**
	 * @param imgUrls 图片url集合
	 * @param response Servlet响应
	 * @param zipName 压缩包名称
	 * @return
	 * */
	public static boolean downloadToZip(List<String> imgUrls  , HttpServletResponse response,String zipName) throws IOException {
		response.setHeader("content-disposition","attachment;filename=" + URLEncoder.encode(zipName+".zip", "UTF-8"));
		OutputStream outputStream = response.getOutputStream();
		ZipOutputStream zos = new ZipOutputStream(outputStream);
		boolean flag = false;
		//输入
		InputStream fis = null;
		BufferedInputStream bis = null;
		// 10K的数据缓冲
		byte[] bufs = new byte[1024*10];
		for (String imgurl : imgUrls) {
			String imgName = imgurl.substring(imgurl.lastIndexOf("/")+1);
			// 构造URL
			URL url = new URL(imgurl);
			// 打开连接
			URLConnection con = url.openConnection();
			//设置请求超时为5s
			con.setConnectTimeout(5 * 1000);
			//读取待压缩的文件并写进压缩包里
			fis = con.getInputStream();
			bis = new BufferedInputStream(fis, 1024*10);

			//创建ZIP实体，并添加进压缩包
			ZipEntry zipEntry = new ZipEntry(imgName);
			zos.putNextEntry(zipEntry);
			int read = 0;
			while((read=bis.read(bufs, 0, 1024*10)) != -1){
				zos.write(bufs,0,read);
			}
			flag = true;
		}
		//关闭流
		try {
			if(null != bis) bis.close();
			if(null != zos) zos.close();
			if(null != fis) fis.close();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return flag;
	}

	/**
	 * 字符串转时间
	 * @param str
	 * @return
	 */
	public static Date StrToDate(String str,String pattern) {
		if (StringUtil.isBlank(str)||StringUtil.isBlank(pattern)){
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * 时间是否在某一个时间段的范围内
	 * @param now   要比较的时间
	 * @param start   开始时间
	 * @param end   结束时间
	 * @return   true在时间段内，false不在时间段内
	 */
	public static boolean hourMinuteBetween(Date now , Date start, Date end) {

		long nowTime = now.getTime();
		long startTime = start.getTime();
		long endTime = end.getTime();

		return nowTime >= startTime && nowTime <= endTime;
	}

	public static String processingTime(Date date , String pattern) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		return simpleDateFormat.format(date);
	}

	/**获取当前时间到第二天凌晨的秒数*/
	public static Long getSecondsNextEarlyMorning() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
	}

}
